//
//  ScrollViewController.m
//  旅游咨询
//
//  Created by Letme on 15-4-22.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "ScrollViewController.h"

@interface ScrollViewController ()

@end

@implementation ScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.myScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view.backgroundColor = [UIColor blackColor];
    
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 110)];
    //black    addSubview
    [self.view addSubview:self.myScrollView];
    //
    self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * self.allPicUrlArray.count + 100, 0);
    self.myScrollView.contentOffset = CGPointMake(self.myScrollView.frame.size.width * self.numberOfGesture, 0);
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    //
    self.myScrollView.delegate = self;
    
    for (NSInteger i = 0; i < self.allPicUrlArray.count; i++) {
        
        NSDictionary *dic = [self.photoInfoArray objectAtIndex:i];
        
        CGFloat h = [[dic objectForKey:@"h"] integerValue];
        CGFloat w = [[dic objectForKey:@"w"] integerValue];
        CGFloat newH = self.myScrollView.frame.size.width * h / w;
        CGFloat y = (self.myScrollView.frame.size.height - newH) / 2;
        UIImageView  *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width * i, y, self.myScrollView.frame.size.width, newH)];
        UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width * i, 0, self.view.frame.size.width, self.myScrollView.frame.size.height)];
       loadingLabel.text = @"loading...";
        [self.myScrollView addSubview:loadingLabel];
        
        NSString *picUrl = [self.allPicUrlArray objectAtIndex:i];
        NSLog(@"%@", picUrl);
        NSURL *url = [NSURL URLWithString:picUrl];
        [myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"06b1OOOPICf5.jpg"]];
        [self.myScrollView addSubview:myImageView];
        NSLog(@"天机");
        NSLog(@"%f", self.myScrollView.frame.size.height);
    }
    
    NSLog(@"%f", self.myScrollView.contentOffset.x);
    
    
    
    
//    if (self.myScrollView.contentOffset.x > self.allPicUrlArray.count * self.myScrollView.frame.size.width) {
//        NSLog(@"AFN.....");
//        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//        
//        NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@/photos/?start=%ld&count=21&gallery_mode=true", self.urlForAFN, self.allPicUrlArray.count];
//#warning
//        //NSLog(@".........%@", self.url_str);
//        
//        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
//        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//            [netWorkManager stopMonitoring];
//            
//            //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
//            
//            
//            NSMutableDictionary *dic = responseObject;
//            
//            NSMutableArray *array = [dic objectForKey:@"items"];
//            
//            for (NSDictionary *smallDic in array) {
//                NSString *image_url = [smallDic objectForKey:@"photo"];
//                [self.allPicUrlArray addObject:image_url];
//            }
//            
//            self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * self.allPicUrlArray.count, 0);
//            self.myScrollView.contentOffset = CGPointMake(self.myScrollView.frame.size.width * (self.allPicUrlArray.count - 21), 0);
//            //
//            for (NSInteger i = (self.allPicUrlArray.count - 21); i < self.allPicUrlArray.count; i++) {
//                
//                
//                UIImageView  *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width * i, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height)];
//                
//                NSString *picUrl = [self.allPicUrlArray objectAtIndex:i];
//                NSLog(@"%@", picUrl);
//                NSURL *url = [NSURL URLWithString:picUrl];
//                [myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"06b1OOOPICf5.jpg"]];
//                [self.myScrollView addSubview:myImageView];
//                NSLog(@"AFN添加");
//            }
//            
//        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            NSLog(@"失败==== %@",error);
//        }];
//    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"拖动将要开始时");
    NSLog(@"%f.........%f", self.myScrollView.contentOffset.x + self.myScrollView.frame.size.width, self.allPicUrlArray.count * self.myScrollView.frame.size.width - self.myScrollView.frame.size.width);
    
    if (self.myScrollView.contentOffset.x + self.myScrollView.frame.size.width > self.allPicUrlArray.count * self.myScrollView.frame.size.width - self.myScrollView.frame.size.width) {
        NSLog(@"AFN.....");
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        
        NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@/photos/?start=%ld&count=21&gallery_mode=true", self.urlForAFN, self.allPicUrlArray.count];
#warning
        //NSLog(@".........%@", self.url_str);
        
        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
            
            //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
            
            
            NSMutableDictionary *dic = responseObject;
            
            NSMutableArray *array = [dic objectForKey:@"items"];
            
            for (NSDictionary *smallDic in array) {
                NSString *image_url = [smallDic objectForKey:@"photo"];
                [self.allPicUrlArray addObject:image_url];
                NSDictionary *dic = [smallDic objectForKey:@"photo_info"];
                [self.photoInfoArray addObject:dic];
                //NSLog(@"%ld", self.picArray.count);
                NSString *headname = [smallDic objectForKey:@"trip_name"];
                [self.tripNameArray addObject:headname];
            }
            
            self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * self.allPicUrlArray.count, 0);
            self.myScrollView.contentOffset = CGPointMake(self.myScrollView.frame.size.width * (self.allPicUrlArray.count - 21), 0);
            
            //
            for (NSInteger i = (self.allPicUrlArray.count - 21); i < self.allPicUrlArray.count; i++) {
                
                NSDictionary *dic = [self.photoInfoArray objectAtIndex:i];
                
                CGFloat h = [[dic objectForKey:@"h"] integerValue];
                CGFloat w = [[dic objectForKey:@"w"] integerValue];
                CGFloat newH = self.myScrollView.frame.size.width * h / w;
                CGFloat y = (self.myScrollView.frame.size.height - newH) / 2;
                UIImageView  *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width * i, y, self.myScrollView.frame.size.width, newH)];
                
                UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width * i, 0, self.view.frame.size.width, self.myScrollView.frame.size.height)];
                loadingLabel.text = @"loading...";
                loadingLabel.textAlignment =  NSTextAlignmentCenter;
                loadingLabel.textColor = [UIColor whiteColor];
                [self.myScrollView addSubview:loadingLabel];
                
//                UIImageView  *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.myScrollView.frame.size.width * i, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height)];
                
                NSString *picUrl = [self.allPicUrlArray objectAtIndex:i];
                NSLog(@"%@", picUrl);
                NSURL *url = [NSURL URLWithString:picUrl];
                [myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"06b1OOOPICf5.jpg"]];
                [self.myScrollView addSubview:myImageView];
                NSLog(@"AFN添加");
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"失败==== %@",error);
        }];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
